/*
 * Copyright 2009 Colin Prinn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.recursv.motific.at;

import net.recursv.motific.at.command.AtCommand;
import net.recursv.motific.at.runner.AtRunnerObserver;
import net.recursv.motific.at.runner.AtScript;
import net.recursv.motific.at.runner.AtScriptObserver;
import net.recursv.motific.utils.StringList;

import javax.microedition.lcdui.*;

/**
 * Created by: recursv
 * Date: 9/08/2009
 * Time: 12:17:24 AM
 */
class AtDisplay extends Canvas implements CommandListener, AtScriptObserver, AtRunnerObserver {
    private static final int WHITE = 0xFFFFFF;
    //    private static final int GREEN = 0x00FF00;
    //    private static final int RED = 0xFF0000;
    private static final int TOP_LEFT = Graphics.TOP | Graphics.LEFT;
    private Image _transparency;
    private AtMidlet _midlet;
    private int _xOffset = 5;
    private StringList _log = new StringList();
    private AtScript _script = AtScript.NULL;
    private static final int TRANSPARENT_COLOR = 0x78c2c2c2;
    private static final int FAILED_TRANSPARENT_COLOR = 0x58FF0000;
    private static final int SUCCESS_TRANSPARENT_COLOR = 0x2000FF00;
    private int _transparentColor = TRANSPARENT_COLOR;
    private int _logDisplayWidth;
    private Font _font;
    private Font _currentItemFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
    private static final String FAILURE = "F A I L U R E";
    private static final String SUCCESS = "S U C C E S S";
    private boolean _success;
    private boolean _failure;

    public AtDisplay(AtMidlet midlet) {
        _midlet = midlet;
        addCommand(new Command("Exit", Command.EXIT, 0));
        setCommandListener(this);
        _font = Font.getDefaultFont();
    }

    protected void paint(Graphics g) {
        _script.paint(g);
        g.setFont(_font);

        g.drawImage(_transparency, 0, 0, TOP_LEFT);

        g.setColor(WHITE);
        for (int index = 0, yOffset = 5; index < _log.size(); index++, yOffset += _font.getHeight()) {
            if (index == _log.size() - 1)
                g.setFont(_currentItemFont);
            g.drawString(_log.elementAt(index), _xOffset, yOffset, TOP_LEFT);
        }

        if (_success)
            g.drawString(SUCCESS, getWidth() / 2 - g.getFont().stringWidth(SUCCESS), getHeight() / 2 - g.getFont().getHeight(), TOP_LEFT);
        else if (_failure)
            g.drawString(FAILURE, getWidth() / 2 - g.getFont().stringWidth(FAILURE), getHeight() / 2 - g.getFont().getHeight(), TOP_LEFT);
    }

    protected void sizeChanged(int i, int i1) {
        createTransparency();
        calculateLogDisplayWidth();
    }

    protected void showNotify() {
        createTransparency();
        calculateLogDisplayWidth();
    }

    private void createTransparency() {
        int[] data = new int[getWidth() * getHeight()];
        for (int index = 0; index < data.length; index++)
            data[index] = _transparentColor;
        _transparency = Image.createRGBImage(data, getWidth(), getHeight(), true);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command.getCommandType() == Command.EXIT)
            _midlet.notifyDestroyed();
    }

    public void notifySuccess(AtCommand command) {
        _log.add(command.toString(), _font, _logDisplayWidth);
        repaint();
    }

    private void calculateLogDisplayWidth() {
        _logDisplayWidth = getWidth() - _xOffset * 2;
    }

    public void notifyFailed(AtCommand command, Throwable error) {
        _log.add(command.toString(), _font, _logDisplayWidth);
        _transparentColor = FAILED_TRANSPARENT_COLOR;
        createTransparency();
        repaint();
        _failure = true;
    }

    public void notifyComplete(AtScript script) {
        _log.removeAll();
        _script = AtScript.NULL;
        repaint();
    }

    public void notifyStarting(AtScript script) {
        _script = script;
        _log.add("Starting " + script.getTitle(), _font, _logDisplayWidth);
        repaint();
    }

    public void notifyStartOfTesting() {
    }

    public void notifyEndOfTesting() {
        if (_transparentColor != FAILED_TRANSPARENT_COLOR) {
            _transparentColor = SUCCESS_TRANSPARENT_COLOR;
            createTransparency();
            repaint();
            _success = true;
        }
    }
}
